import {
  customerapplycloseslideorder,
  customerapplycloseconfirmorder,
  customeracceptcloseslideorder,
  customeracceptcloseconfirmorder,
  customerrejectcloseslideorder,
  customerrejectcloseconfirmorder,
  providerapplycloseslideorder,
  providerapplycloseconfirmorder,
  provideracceptcloseslideorder,
  provideracceptcloseconfirmorder,
  providerrejectcloseslideorder,
  providerrejectcloseconfirmorder
} from "@/api/order/close";
import quotationDict from "@/dict/quotation";

export const btnClose = [
  // 买家 关闭订单
  // 点价
  {
    name: "申请关闭",
    clickEvent: "buyerApplyPricingClose",
    flag: "buyerApplyPricingClose"
  },
  {
    name: "同意关闭",
    clickEvent: "buyerAcceptPricingClose",
    flag: "buyerAcceptPricingClose"
  },
  {
    name: "拒绝关闭",
    clickEvent: "buyerRejectPricingClose",
    flag: "buyerRejectPricingClose"
  },
  // 确定价
  {
    name: "申请关闭",
    clickEvent: "buyerApplyConfirmClose",
    flag: "buyerApplyConfirmClose"
  },
  {
    name: "同意关闭",
    clickEvent: "buyerAcceptConfirmClose",
    flag: "buyerAcceptConfirmClose"
  },
  {
    name: "拒绝关闭",
    clickEvent: "buyerRejectConfirmClose",
    flag: "buyerRejectConfirmClose"
  },
  // 卖家 关闭订单
  // 点价
  {
    name: "申请关闭",
    clickEvent: "sellerApplyPricingClose",
    flag: "sellerApplyPricingClose"
  },
  {
    name: "同意关闭",
    clickEvent: "sellerAcceptPricingClose",
    flag: "sellerAcceptPricingClose"
  },
  {
    name: "拒绝关闭",
    clickEvent: "sellerRejectPricingClose",
    flag: "sellerRejectPricingClose"
  },
  // 确定价
  {
    name: "申请关闭",
    clickEvent: "sellerApplyConfirmClose",
    flag: "sellerApplyConfirmClose"
  },
  {
    name: "同意关闭",
    clickEvent: "sellerAcceptConfirmClose",
    flag: "sellerAcceptConfirmClose"
  },
  {
    name: "拒绝关闭",
    clickEvent: "sellerRejectConfirmClose",
    flag: "sellerRejectConfirmClose"
  }
];

export const close = {
  data() {
    return {
      colseReason: undefined // 关闭订单理由
    };
  },
  methods: {
    // 申请关闭
    applyClose(row, type) {
      let _this = this;
      let h = this.$createElement;
      this.$confirm({
        title: "申请关闭订单",
        icon: "exclamation-circle",
        width: 540,
        content: h("div", {}, [
          h(
            "p",
            "是否申请关闭订单，申请后需要对方管理员同意才可以成功关闭订单。"
          ),
          h(
            "p",
            "订单关闭后，不可恢复，表示该订单已终止，不能再生成合同，同时，该订单已扣减的商品库存、关联的实物库存及已扣减的额度，均会返回。"
          ),
          h("p", "若确认申请关闭订单，请输入关闭订单理由："),
          h("a-textarea", {
            attrs: {
              placeholder: "请输入关闭订单的理由"
            },
            domProps: {
              value: _this.colseReason
            },
            on: {
              input: function(event) {
                _this.colseReason = event.target.value;
              }
            }
          })
        ]),
        onOk() {
          return new Promise((resolve, reject) => {
            let request;
            let requestFn = function() {
              request({
                orderId: row.id,
                remark: _this.colseReason,
                version: row.version
              })
                .then(res => {
                  _this.$message.success("操作成功");
                  _this.$emit("handleSuccess", {
                    data: res,
                    type:
                      type === 0
                        ? row.priceType ===
                          quotationDict.priceType.pricing.value
                          ? "buyerApplyPricingClose"
                          : "buyerApplyConfirmClose"
                        : row.priceType ===
                          quotationDict.priceType.pricing.value
                        ? "sellerApplyPricingClose"
                        : "sellerApplyConfirmClose"
                  });
                  resolve();
                })
                .catch(() => {
                  reject();
                });
            };
            // 区分作价方式，买卖家
            if (row.priceType === quotationDict.priceType.pricing.value) {
              // 点价
              request =
                type === 0
                  ? customerapplycloseslideorder
                  : providerapplycloseslideorder;
              requestFn();
            } else {
              // 确定价
              request =
                type === 0
                  ? customerapplycloseconfirmorder
                  : providerapplycloseconfirmorder;
              requestFn();
            }
          });
        },
        onCancel() {
          _this.colseReason = undefined;
        }
      });
    },
    // 买家申请关闭点价
    buyerApplyPricingClose(row) {
      this.applyClose(row, 0);
    },
    // 买家申请关闭确定价
    buyerApplyConfirmClose(row) {
      this.applyClose(row, 0);
    },
    // 卖家申请关闭点价
    sellerApplyPricingClose(row) {
      this.applyClose(row, 1);
    },
    // 卖家申请关闭确定价
    sellerApplyConfirmClose(row) {
      this.applyClose(row, 1);
    },
    // 买家同意关闭点价订单
    buyerAcceptPricingClose(row) {
      this.buyerAcceptClose(row);
    },
    // 买家同意关闭确定价订单
    buyerAcceptConfirmClose(row) {
      this.buyerAcceptClose(row);
    },
    // 买家同意关闭订单
    buyerAcceptClose(row) {
      let request =
        row.priceType === quotationDict.priceType.pricing.value
          ? customeracceptcloseslideorder
          : customeracceptcloseconfirmorder;
      request({
        orderId: row.id,
        version: row.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", {
          data: res,
          type:
            row.priceType === quotationDict.priceType.pricing.value
              ? "buyerAcceptPricingClose"
              : "buyerAcceptConfirmClose"
        });
      });
    },
    // 买家拒绝关闭点价订单
    buyerRejectPricingClose(row) {
      this.buyerRejectClose(row);
    },
    // 买家拒绝关闭确定价订单
    buyerRejectConfirmClose(row) {
      this.buyerRejectClose(row);
    },
    // 买家拒绝关闭
    buyerRejectClose(row) {
      let request =
        row.priceType === quotationDict.priceType.pricing.value
          ? customerrejectcloseslideorder
          : customerrejectcloseconfirmorder;
      request({
        orderId: row.id,
        version: row.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", {
          data: res,
          type:
            row.priceType === quotationDict.priceType.pricing.value
              ? "buyerRejectPricingClose"
              : "buyerRejectConfirmClose"
        });
      });
    },
    // 卖家同意关闭点价订单
    sellerAcceptPricingClose(row) {
      this.sellerAcceptClose(row);
    },
    // 卖家同意关闭确定价订单
    sellerAcceptConfirmClose(row) {
      this.sellerAcceptClose(row);
    },
    // 卖家同意关闭
    sellerAcceptClose(row) {
      let request =
        row.priceType === quotationDict.priceType.pricing.value
          ? provideracceptcloseslideorder
          : provideracceptcloseconfirmorder;
      request({
        orderId: row.id,
        version: row.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", {
          data: res,
          type:
            row.priceType === quotationDict.priceType.pricing.value
              ? "sellerAcceptPricingClose"
              : "sellerAcceptConfirmClose"
        });
      });
    },
    // 卖家拒绝关闭点价订单
    sellerRejectPricingClose(row) {
      this.sellerRejectClose(row);
    },
    // 卖家拒绝关闭确定价订单
    sellerRejectConfirmClose(row) {
      this.sellerRejectClose(row);
    },
    // 卖家拒绝关闭
    sellerRejectClose(row) {
      let request =
        row.priceType === quotationDict.priceType.pricing.value
          ? providerrejectcloseslideorder
          : providerrejectcloseconfirmorder;
      request({
        orderId: row.id,
        version: row.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", {
          data: res,
          type:
            row.priceType === quotationDict.priceType.pricing.value
              ? "sellerRejectPricingClose"
              : "sellerRejectConfirmClose"
        });
      });
    }
  }
};
